<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$errors = array();

if ($route->POST("action") == 'edit') {

	$banners_items = $route->POST("banners");

	foreach ($banners_items as $key => $banner) {
		$power = isset($banner['power']) ? "1" : "0";
		$query = "update `banners` set `text`='".UV::readMQ($banner['text'])."',`power`='$power' where `id` = '".UV::readInt($banner['id'])."' limit 1";

		$update = $db->sendQuery($query);
		if (!$update) {
			$errors[] = "Ошибка обновления баннера $banner[id]";
		}
	}
}

$banners = $db->getTable("select * from `banners` order by `id`");

loadPage("head.php");
?>
<h1>Настройка баннеров</h1><br /><?php
	if (count($errors) > 0)
		echo "<p class='fail'>".implode("<br />",$errors)."<br /><br /></p>";
	elseif ($route->POST("action") == 'edit')
		echo "<p class='success'>Баннеры успешно обновлены<br /><br /></p>";
?>
<form method='POST' id="edit">
	<input type='hidden' name='action' value='edit' />
	<table cellpadding='3' cellspacing='5'>
		<tr style="font-weight: bold">
			<td width='170px'>Положение</td>
			<td width='600px'>Содержимое</td>
			<td width='50px'>Включен</td>
		</tr>
		<?php
		foreach ($banners as $key=>$banner) {
			?>
			<tr>
				<td><label for="banner_<?php echo $banner['id']; ?>"><?php echo $banner['name']; ?></label></td>
				<td>
					<textarea id='banner_<?php echo $banner['id']; ?>' name='banners[<?php echo $key;?>][text]'><?php echo $banner['text']; ?></textarea>
				</td>
				<td align="center">
					<input type="checkbox" name="banners[<?php echo $key;?>][power]" <?php if ($banner['power']) echo "checked='checked'"; ?> />
					<input type="hidden" name="banners[<?php echo $key;?>][id]" value='<?php echo $banner['id'] ?>' />
				</td>
			</tr>
			<?php
		}
		?>
		<tr>
			<td colspan='3' align='right'><input type='submit' value='Обновить' /></td>
		</tr>
	</table>
</form>

<?php loadPage("footer.php"); ?>